<template>
	<view>
		<view class="search-box">
			<uni-search-bar :radius="100" cancelButton="none" @input="input"></uni-search-bar>
		</view>
		<view class="sugg-list" v-if="searchResults.length">
			<view class="sugg-item" v-for="(item,index) in searchResults" :key="index" @click="gotDetails(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="16" @click="cleanHistory"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag inverted class="history-list-item" v-for="(item,index) in historys" :key="index" :text="item"
					@click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 定时器开关
				timer: null,
				// 关键词
				kw: '',
				// 搜索结果列表
				searchResults: [],
				// 搜索历史表数据
				historyList: []
			}
		},
		computed: {
			historys() {
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			// 从本地取出
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			/* 
			输入事件 
			 */
			input(e) {
				// 关闭定时器
				clearTimeout(this.timer)
				// 防抖处理
				this.timer = setTimeout(() => {
					// 如果500毫秒呢,没有触发,及收集关键词
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			/* 
			 获取关键字数据列表
			 */
			async getSearchList() {
				if (!this.kw) return this.searchResults = []
				const data = {
					query: this.kw
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', data)
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message || []
				this.saveSearchHistory()
			},
			/* 
			点击列表跳转到商品详情 
			 */
			gotDetails(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			/* 
			 保存关键词 
			 */
			saveSearchHistory() {
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				// 先删除后添加
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				// 存储到本地
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			/* 
			 删除关键字
			 */
			cleanHistory() {
				// 二次确认框
				// uni.showModal({
				// 	title: '提示',
				// 	content: '是否删除最近搜索历史记录',
				// 	success: function(res) {
				// 		if (res.confirm) {
				// 			// 确定之后删除本地记录
				// 			this.historyList = []
				// 			uni.setStorageSync('kw', '[]')
				// 			console.log(this.historyList, '数据')
				// 		}
				// 	}
				// })

				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			/* 
			 通过历史记录跳转到商品列表
			 */
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;

		.uni-searchbar {
			background-color: #c00000;
		}
	}

	.sugg-list {
		padding: 0 6px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			font-size: 13px;
			height: 40px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.history-list-item {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>